/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package demo.parallelcore;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/*
 * DataUtilities is a singleton helper class that creates test data. This class
 * uses jaxws API to read xml into java object, which includes 3 list of data
 * 1. Customers
 * 2. Products
 * 3. Suppliers
 * Note in this demo here we don't use stream API to change data, if we use
 * stream API to populate source data, we should be aware this is not free and
 * not thread-safe.
*/

interface XMLList<E>{
    public List<E> getList();
}

/*
 * class for PRODUCTS_XML's root
*/
@XmlRootElement(name = "products")
class Products implements XMLList<Product> {
    /*
     * node products only contains product list
    */
    @XmlElement(name = "product")
    private ArrayList<Product> products;

    /*
     * product list
    */
    @Override
    public List<Product> getList() {
        return products;
    }
}

/*
 * class for SUPPLIERS_XML's root
*/
@XmlRootElement(name = "suppliers")
class Suppliers implements XMLList<Supplier> {
    /*
     * node suppliers only contains supplier list
    */
    @Override
    public List<Supplier> getList() {
        return suppliers;
    }

    /*
     * supplier list
    */
    @XmlElement(name = "supplier")
    private ArrayList<Supplier> suppliers;
}

/*
 * class for CUSTOMERS_XML's root
 */
@XmlRootElement(name = "customers")
class Customers implements XMLList<Customer> {
    /*
     * node customers only contains customer list
    */
    @Override
    public List<Customer> getList() {
        return customers;
    }

    /*
     * customer list
    */
    @XmlElement(name = "customer")
    private ArrayList<Customer> customers;
}

public class DataUtilities {
    /*
     * xml file name for Customers object
    */
    private final static String CUSTOMERS_XML = "Customers.xml";

    /*
     * xml file name for Products object
    */
    private final static String PRODUCTS_XML  = "Products.xml";

    /*
     * xml file name for Products object
    */
    private final static String SUPPLIERS_XML = "Suppliers.xml";

    /*
     * Lazy initiate static inner class for 
    */
    private static class UtilitiesHolder {    
        static DataUtilities instance = new DataUtilities();    
    }

    //private constructor is needed for singleton
    private DataUtilities(){}

    //products
    private List<Product> productList;

    //customer
    private List<Customer> customerList;

    //suppliers
    private List<Supplier> supplierList;

    /*
     * Lazy initial to get product list
    */
    public List<Product> getProductList() {
        if (productList == null) {
            createLists();
        }
        return productList;
    }

    /*
     * Lazy initial to get customer list
    */
    public List<Customer> getCustomerList() {
        if (customerList == null) {
            createLists();
        }
        return customerList;
    }

    /*
     * Lazy initial to get supplier list
    */
    public List<Supplier> getSupplierList() {
        if (supplierList == null) {
            createLists();
        }
        return supplierList;
    }

    /*
     * convert xml files to java objects
    */
    private void createLists() { 
        customerList = xmlToObject(Customers.class, CUSTOMERS_XML);
        productList = xmlToObject(Products.class, PRODUCTS_XML);
        supplierList =  xmlToObject(Suppliers.class, SUPPLIERS_XML);
    }
    
    /*
     * Convert a xml file to instance of class by given file name
     * @param clazz  to be converted java object's class
     * @param file   xml file name
     * @return a list of object that xml's root contains
    */
    private <U, V extends XMLList<U>> List<U> xmlToObject(Class<V> clazz, 
            String file) {
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller um = context.createUnmarshaller();
            @SuppressWarnings("unchecked")
            V cs = (V) um.unmarshal(new File(file));
            return cs.getList();
        } catch (JAXBException ex) {
            throw new RuntimeException(ex);
        }   
    }

    /*
     * static factory method for singleton
    */
    public static DataUtilities getUtilities(){
        return UtilitiesHolder.instance;
    }
}
